package cn.rht.study.util.redis;


import cn.rht.study.config.RedisConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;

import javax.annotation.PostConstruct;
import java.util.logging.Logger;

@Component
public class JedisCache {
	
	protected Logger log = Logger.getLogger("JedisCache");

    protected JedisPool pool;

    @Autowired
    private RedisConfig redisConfig;
    //protected JedisSentinelPool pool;
    
    public JedisCache(){
    }

    @PostConstruct
    public void init() {
        pool = redisConfig.redisPoolFactory();
    }

    public JedisCache( JedisPool pool ){
    	this.pool = pool;
    }
   
    public JedisHashCache getJedisHashCache( String key ){
    	return new JedisHashCache( pool, key );
    }
    
    public JedisStringCache getJedisStringCache( String key ){
        try {
            JedisStringCache jedisStringCache = new JedisStringCache(pool, key);
            return jedisStringCache;
        } catch (Exception e) {
            System.err.println("redis链接异常");
            e.printStackTrace();
        }
        return null;
    }
    
    public JedisSetCache getJedisSetCache(String key){
    	JedisSetCache jedisSetCache = new JedisSetCache(pool,key);
    	return jedisSetCache;
    }
    public void destroy() {
        pool.destroy();
    }

    public JedisPool getPool() {
        return pool;
    }

    public void setPool(JedisPool pool) {
        this.pool = pool;
    }


}
